package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 梦龙
 * @date 2023/11/30 10:31
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * @description: 两个任务：
     * 1.添加套餐信息到套餐表
     * 2.添加套餐和菜品的关联数据到套餐-菜品表
     * @return: void
     * @author: 梦龙
     * @time: 2023/11/30 11:11
     */
    @Override
    @Transactional
    public void saveSetmealWithDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //属性复制
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //添加套餐信息到套餐表
        setmealMapper.insert(setmeal);

        //获取套餐id
        Long id = setmeal.getId();

        //拿出关联的菜品信息
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();

        if (dishes != null && dishes.size() > 0) {
            //使用lambda表达式来遍历为每一个菜品信息给属性setmealId赋值
            dishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });

            //批量插入菜品数据
            setMealDishMapper.insert(dishes);
        }
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //然后删除套餐
        for (Long id : ids) {
            //先检查是否有起售的套餐，不是停售我不删。
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //先删除套餐-菜品表里面的菜品信息
        setMealDishMapper.deleteByIds(ids);
        //再通过id集删除套餐信息
        setmealMapper.deleteByIds(ids);
    }

    /**
     * @description:修改套餐内容
     * @return: com.sky.vo.SetmealVO
     * @time: 2023/11/30 16:25
     */
    @Override
    public void update(SetmealDTO setmealDTO) {

        //将剩余数据修改
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //先把id取出来，然后将所有关联的菜品删掉
        List<Long> ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        setMealDishMapper.deleteByIds(ids);

        //然后取出dto里面的菜品，将其添加到套餐-菜品表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setMealDishMapper.insert(setmealDishes);
        }
    }

    /**
     * @description:根据id查找套餐信息
     * @return: com.sky.vo.SetmealVO
     * @time: 2023/11/30 20:14
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setMealDishMapper.getDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal().builder()
                .id(id)
                .status(status)
                .build();//链式调用
        List<SetmealDish> setmealDishes = setMealDishMapper.getDishBySetmealId(id);
        //如果status要更新为起售
        if (status == StatusConstant.ENABLE) {
            for (SetmealDish setmealDish : setmealDishes) {
                //通过菜品id获取菜品status的值
                Integer dishStatus = dishMapper.getStatusById(setmealDish.getDishId());
                //如果有菜品是停售的
                if (dishStatus == StatusConstant.DISABLE) {
                    //那么直接抛异常交给异常处理器进行处理
                    //因为有停售的菜品无法让套餐起售
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
